using Microsoft.AspNetCore.Mvc;
using RealtimeCollabGame.Application.Services;
using RealtimeCollabGame.Application.Commands;
using RealtimeCollabGame.Application.DTOs;
using Microsoft.Extensions.DependencyInjection;
using RealtimeCollabGame.Infrastructure.Persistence;
using Microsoft.EntityFrameworkCore;

namespace RealtimeCollabGame.API.Controllers;

[ApiController]
[Route("api/[controller]")]
public class GameController : ControllerBase
{
    private readonly GameService _gameService;

    public GameController(GameService gameService)
    {
        _gameService = gameService;
    }

    [HttpPost("create")]
    public async Task<ActionResult<GameDto>> CreateGame([FromBody] CreateGameCommand command)
    {
        try
        {
            var game = await _gameService.CreateGameAsync(command);
            return Ok(game);
        }
        catch (Exception ex)
        {
            return BadRequest(new { error = ex.Message });
        }
    }

    [HttpPost("join")]
    public async Task<ActionResult<GameDto>> JoinGame([FromBody] JoinGameCommand command)
    {
        try
        {
            var game = await _gameService.JoinGameAsync(command);
            return Ok(game);
        }
        catch (Exception ex)
        {
            return BadRequest(new { error = ex.Message });
        }
    }

    [HttpPost("leave")]
    public async Task<ActionResult<GameDto>> LeaveGame([FromBody] LeaveGameCommand command)
    {
        try
        {
            var game = await _gameService.LeaveGameAsync(command);
            return Ok(game);
        }
        catch (Exception ex)
        {
            return BadRequest(new { error = ex.Message });
        }
    }

    [HttpGet("{id}")]
    public async Task<ActionResult<GameDto>> GetGame(Guid id)
    {
        var game = await _gameService.GetGameAsync(id);
        if (game == null)
            return NotFound();

        return Ok(game);
    }

    [HttpGet("code/{code}")]
    public async Task<ActionResult<GameDto>> GetGameByCode(string code)
    {
        var game = await _gameService.GetGameByCodeAsync(code);
        if (game == null)
            return NotFound();

        return Ok(game);
    }

    [HttpGet("list")]
    public async Task<ActionResult<List<GameDto>>> GetGameList()
    {
        try
        {
            var games = await _gameService.GetAllGamesAsync();
            return Ok(games);
        }
        catch (Exception ex)
        {
            return BadRequest(new { error = ex.Message });
        }
    }

    [HttpPost("paint")]
    public async Task<ActionResult> PaintAction([FromBody] PaintActionCommand command)
    {
        try
        {
            await _gameService.ProcessPaintActionAsync(command);
            return Ok();
        }
        catch (Exception ex)
        {
            return BadRequest(new { error = ex.Message });
        }
    }

    [HttpPost("ready")]
    public async Task<ActionResult> SetPlayerReady([FromBody] SetPlayerReadyCommand command)
    {
        try
        {
            await _gameService.SetPlayerReadyAsync(command.GameId, command.PlayerId, command.IsReady);
            return Ok();
        }
        catch (Exception ex)
        {
            return BadRequest(new { error = ex.Message });
        }
    }

    [HttpDelete("{id}")]
    public async Task<ActionResult> DeleteGame(Guid id)
    {
        try
        {
            await _gameService.DeleteGameAsync(id);
            return Ok(new { message = "游戏删除成功" });
        }
        catch (Exception ex)
        {
            return BadRequest(new { error = ex.Message });
        }
    }
}